using ERP.Application.RepositoryManage;
using ERP.EntityFrameworkCore;
using ERP.EquipmentManage;
using ERP.HumanResources.HumanResourceManagement;
using ERP.ProductionManage;
using ERP.RBAC;
using ERP.RepositoryManage;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace ERP.Web;

public class Program
{
    public async static Task<int> Main(string[] args)
    {
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
            .Enrich.FromLogContext()
            .WriteTo.Async(c => c.File("Logs/logs.txt"))
            .WriteTo.Async(c => c.Console())
            .CreateLogger();

        try
        {
            Log.Information("Starting web host.");
            var builder = WebApplication.CreateBuilder(args);

            builder.Host.UseAutofac()
                .UseSerilog();
            
            builder.Services.AddTransient<IMenuServices, MenuServices>();
            builder.Services.AddTransient<ILoginServices, LoginServices>();
           
            builder.Services.AddTransient<IProductionDispatchService, ProductionDispatchService>();
            builder.Services.AddTransient<IDvsubjectService, DvsubjectService>();

            builder.Services.AddTransient<IUserServices, UserServices>();
            builder.Services.AddTransient<IPackingService, PackingService>();
            
       

            await builder.AddApplicationAsync<ERPWebModule>();

            var app = builder.Build();

            Log.Information("Initializing application...");
            await app.InitializeApplicationAsync();
            
            Log.Information("Starting application...");
            await app.RunAsync();
            
            return 0;
        }
        catch (Exception ex)
        {
            Log.Fatal(ex, "Application terminated unexpectedly!");
            return 1;
        }
        finally
        {
            Log.CloseAndFlush();
        }
    }
}
